package com.mcy.springbatch.job;

import com.mcy.springbatch.entity.SysUser;
import com.mcy.springbatch.reader.ReadUserDataFlatFileItemReader;
import com.mcy.springbatch.reader.ReadUserDataJpaPagingItemReader;
import com.mcy.springbatch.writer.SysUserItemWriter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

/**
 * 多线程执行job
 * @author : Create By mcy
 * @date : 2019/8/26 16:11:13
 */
@Slf4j
@Configuration
public class QMultiThreadJobConfig extends BaseJobConfig {

    @Autowired
    ReadUserDataFlatFileItemReader readUserDataFlatFileItemReader;

    @Autowired
    private ReadUserDataJpaPagingItemReader readUserDataJpaPagingItemReader;

    @Autowired
    private SysUserItemWriter sysUserItemWriter;

    @Bean
    public Job multiThreadJob(){
        return jobBuilderFactory.get("multiThreadJob")
                .start(multiThreadJobStep())
                .build();
    }

    @Bean
    public Step multiThreadJobStep(){
        return stepBuilderFactory.get("multiThreadJobStep")
                .<SysUser,SysUser>chunk(10)
                .reader(readUserDataJpaPagingItemReader)
                .writer(sysUserItemWriter)
                .throttleLimit(4)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .build();
    }
}
